<?php

namespace App\Remote\Mybank;

use App\Exceptions\BaseException;
use App\Kuafu\Mybank\Enum\MybankFunctionEnum;
use App\Kuafu\Mybank\Model\MybankOrderWithholdRecordModel;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

/**
 * Mybank 网商银行代扣
 *
 * Doc https://loan-platform.yuque.com/vo42xq/dpd6q7/tgfvw7mzudvixs9b#T4wjZ
 */
class MybankCloudFundsRemote extends Remote
{


    /**
     * 协议代扣
     *
     * @param string $outTradeNo 代扣业务订单号
     * @param string $totalAmount 交易金额（分）
     * @param string $freezeAmount 冻结部分出金（分）
     * @param string $availableAmount 可用部分金额（分）
     * @param string $payerId 付款方商户号
     * @param string $payeeId 收款方ID
     * @param string $payeeType 收款方类型 MERCHANT / PLATFORM
     * @param string $payerType 付款方类型：MERCHANT
     * @param string $scene 业务场景：品牌协议代扣场景：PROTOCOL_WITHHOLD 品牌协议代扣支持冻结场景： PROTOCOL_WITHHOLD_SUPPORT_FREEZE
     * @return mixed
     * @throws GuzzleException
     * @throws BaseException
     */
    public static function withholdApply(
        string $outTradeNo,
        string $totalAmount,
        string $freezeAmount,
        string $availableAmount,
        string $payerId,
        string $payeeId = '',
        string $payeeType = MybankOrderWithholdRecordModel::WITHHOLD_PLATFORM,
        string $payerType = MybankOrderWithholdRecordModel::WITHHOLD_MERCHANT,
        string $scene = MybankOrderWithholdRecordModel::WITHHOLD_SCENE_FREEZE
    ) {
        if (empty($payeeId)) {
            $payeeId = Remote::getIsvOrgId();
        }
        $params = [
            'IsvOrgId'        => Remote::getIsvOrgId(),
            'OutTradeNo'      => $outTradeNo,             // 代扣业务订单号
            'Scene'           => $scene,
            'TotalAmount'     => $totalAmount, // 交易金额（分）
            'FreezeAmount'    => $freezeAmount, // 冻结部分出金（分）
            'AvailableAmount' => $availableAmount, // 可用部分金额（分）
            'Currency'        => 'CNY', // 币种
            'PayerId'         => $payerId, // 付款方商户号
            'PayerType'       => $payerType, // 付款方类型：MERCHANT
            'PayeeId'         => $payeeId, // 收款方ID
            'PayeeType'       => $payeeType,// 收款方类型 MERCHANT / PLATFORM
        ];
        return self::returnBodyData(self::customRequestV2(
            config('pay.mybank.bkServerUrl'),
            Request::METHOD_POST,
            self::buildXmlRequestData($params, MybankFunctionEnum::BKCLOUDFUNDS_ORDER_WITHHOLD_APPLY)
        ));
    }


    /**
     * 协议代扣
     *
     * @param string $participantType
     * @param string $participantId
     * @param string $relateOrderNo
     * @param string $relateTransNo
     * @param string $outRefundNo
     * @param string $refundAmount
     * @param string $refundReason
     * @param string $extraInfo
     * @param string $memo
     * @return mixed
     * @throws BaseException
     * @throws GuzzleException
     */
    public static function withholdRefundApply(
        string $participantType,
        string $participantId,
        string $relateOrderNo,
        string $relateTransNo,
        string $outRefundNo,
        string $refundAmount,
        string $refundReason,
        string $extraInfo = '',
        string $memo = ''
    ) {
        $params = [
            'IsvOrgId'        => Remote::getIsvOrgId(),
            'ParticipantType' => $participantType,
            'ParticipantId'   => $participantId,
            'RelateOrderNo'   => $relateOrderNo,
            'RelateTransNo'   => $relateTransNo,
            'OutRefundNo'     => $outRefundNo,
            'RefundAmount'    => $refundAmount,
            'Currency'        => 'CNY',
            'RefundReason'    => $refundReason,
            'ExtraInfo'       => $extraInfo,
            'Memo'            => $memo,
        ];

        $params = array_filter($params);

        return self::returnBodyData(self::customRequestV2(
            config('pay.mybank.bkServerUrl'),
            Request::METHOD_POST,
            self::buildXmlRequestData($params, MybankFunctionEnum::BKCLOUDFUNDS_ORDER_WITHHOLD_REFUND_APPLY)
        ));
    }


    /**
     * 协议代扣退款主动查询
     *
     * @param string $participantType
     * @param string $participantId
     * @param string $relateOrderNo
     * @param string $refundOrderNo
     * @param string $outRefundNo
     * @return mixed
     * @throws GuzzleException
     * @throws BaseException
     */
    public static function withholdRefundQuery(
        string $participantType,
        string $participantId,
        string $relateOrderNo,
        string $refundOrderNo,
        string $outRefundNo
    ) {

        $params = [
            'IsvOrgId'        => Remote::getIsvOrgId(),
            'RelateOrderNo'   => $relateOrderNo,
            'ParticipantType' => $participantType,
            'ParticipantId'   => $participantId,
            'OutRefundNo'     => $outRefundNo,
            'RefundOrderNo'   => $refundOrderNo,

        ];

        $params = array_filter($params);

        return self::returnBodyData(self::customRequestV2(
            config('pay.mybank.bkServerUrl'),
            Request::METHOD_POST,
            self::buildXmlRequestData($params, MybankFunctionEnum::BKCLOUDFUNDS_PROTOCOLP_WITHHOLD_REFUND_QUERY)
        ));
    }


    /**
     * 协议代扣退款主动查询
     *
     * @param string $participantType
     * @param string $participantId
     * @param string $relateOrderNo
     * @param string $refundOrderNo
     * @param string $outRefundNo
     * @return mixed
     * @throws GuzzleException
     * @throws BaseException
     */
    public static function withholdQuery(string $operateNo, string $outTradeNo)
    {
        $params = [
            'IsvOrgId'   => Remote::getIsvOrgId(),
            'OperateNo'  => $operateNo,
            'OutTradeNo' => $outTradeNo,
        ];

        $params = array_filter($params);

        return self::returnBodyData(self::customRequestV2(
            config('pay.mybank.bkServerUrl'),
            Request::METHOD_POST,
            self::buildXmlRequestData($params, MybankFunctionEnum::BKCLOUDFUNDS_PROTOCOL_WITHHOLD_QUERY)
        ));
    }


}
